<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      inline
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="联系方式" prop="contactInformation">
        <el-input
          v-model="queryParams.contactInformation"
          placeholder="请输入联系方式"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="状态" prop="state">
        <el-select
          v-model="queryParams.state"
          placeholder="请选择状态"
          clearable
          @change="handleQuery"
        >
          <el-option label="未读" :value="1" />
          <el-option label="已读" :value="2" />
          <el-option label="管理员未读" :value="3" />
          <el-option label="管理员已读" :value="4" />
        </el-select>
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">
          搜索
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">
          重置
        </el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['weiriya:word:edit']"
        >
          回复
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['weiriya:word:remove']"
        >
          删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['weiriya:word:export']"
        >
          导出
        </el-button>
      </el-col>
      <right-toolbar :show-search.sync="showSearch" @query-table="getList" />
    </el-row>

    <el-table
      v-loading="loading"
      :data="wordList"
      border
      stripe
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="#" type="index" width="55" align="center">
        <template slot-scope="scope">
          <span>{{
            (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1
          }}</span>
        </template>
      </el-table-column>
      <el-table-column label="留言内容" align="center">
        <template slot-scope="scope">
          <el-popover placement="top" width="600" trigger="hover">
            <div v-html="scope.row.leaveWordContent"></div>
            <div slot="reference" class="omit" >
              {{ scope.row.leaveWordContent }}
            </div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="留言图片" align="center" prop="imgUrl">
        <template slot-scope="scope">
          <div v-if="scope.row.imgUrl">
            <img
              v-for="(img, index) in scope.row.imgUrl.split(',')"
              :key="index"
              :src="baseUrl + img"
              @click="handlePreview(baseUrl + img)"
              class="preview-img"
            />
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="联系方式"
        align="center"
        width="120"
        prop="contactInformation"
      />
      <el-table-column label="回复内容" prop="showContent" align="center">
        <template slot-scope="scope">
          <el-popover placement="top" width="600" trigger="hover">
            <div v-html="scope.row.replyContent"></div>
            <div slot="reference" class="omit" >
              {{ scope.row.showContent }}
            </div>
          </el-popover>
        </template>
      </el-table-column>
      <!-- <el-table-column label="状态" align="center" width="120">
        <template slot-scope="scope">
          <el-tag
            :type="scope.row.state == 1 || scope.row.state == 3 ? 'danger' : 'success'"
            effect="plain"
          >
            {{
              scope.row.state == 1
                ? "未读"
                : scope.row.state == 2
                ? "已读"
                : scope.row.state == 3
                ? "管理员未读"
                : "管理员已读"
            }}
          </el-tag>
        </template>
      </el-table-column> -->
      <el-table-column label="操作" align="center" width="180">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.state > 2"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['weiriya:word:edit']"
          >
            回复
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['weiriya:word:remove']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog title="回复留言" :visible.sync="open" width="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="留言图片" v-if="form.imgUrl">
          <img
            v-for="(img, index) in form.imgUrl.split(',')"
            :key="index"
            :src="baseUrl + img"
            @click="handlePreview(baseUrl + img)"
            class="preview-img"
          />
        </el-form-item>
        <el-form-item label="留言内容">
          <div v-html="form.leaveWordContent"></div>
        </el-form-item>
        <el-form-item label="联系方式">
          <div>{{ form.contactInformation }}</div>
        </el-form-item>
        <el-form-item label="回复内容" prop="replyContent">
          <editor v-model="form.replyContent" :min-height="192" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-image-viewer
      v-if="prePath"
      :on-close="
        () => {
          prePath = '';
        }
      "
      :url-list="[prePath]"
    />
  </div>
</template>

<script>
import ElImageViewer from "element-ui/packages/image/src/image-viewer";
import { listWord, getWord, delWord, addWord, updateWord } from "@/api/weiriya/word";

export default {
  name: "Word",
  components: { ElImageViewer },
  data() {
    return {
      baseUrl: process.env.VUE_APP_BASE_API,
      loading: false, // 遮罩层
      ids: [], // 选中数组
      single: true, // 非单个禁用
      multiple: true, // 非多个禁用
      showSearch: true, // 显示搜索条件
      total: 0, // 总条数
      wordList: [], // 用户反馈管理表格数据
      title: "", // 弹出层标题
      open: false, // 是否显示弹出层
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        memberId: null,
        imgUrl: null,
        contactInformation: null,
        leaveWordContent: null,
        replyContent: null,
        replyUserId: null,
        state: null,
      },
      form: {}, // 表单参数
      rules: {
        // 表单校验
        replyContent: [{ required: true, message: "请输入回复内容", trigger: "blur" }],
      },
      prePath: "", // 图片预览路径
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 查询用户反馈管理列表
    getList() {
      this.loading = true;
      listWord(this.queryParams)
        .then((res) => {
          res.rows.forEach((item) => {
            item.showContent = item.replyContent
              ? item.replyContent.replace(/<[^>]*>/g, "")
              : "";
          });
          this.wordList = res.rows;
          this.total = res.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        memberId: null,
        imgUrl: null,
        contactInformation: null,
        leaveWordContent: null,
        replyContent: null,
        replyUserId: null,
      };
      this.resetForm("form");
    },
    // 搜索
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 重置
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1 || selection.some((i) => i.state <= 2);
      this.multiple = !selection.length;
    },
    // 预览图片
    handlePreview(path) {
      this.prePath = path;
    },
    // 修改
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getWord(id).then((res) => {
        this.form = res.data;
        this.open = true;
      });
    },
    // 提交
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateWord(this.form).then((res) => {
              this.$modal.msgSuccess("回复成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWord(this.form).then((res) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    // 删除
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除用户反馈管理编号为"' + ids + '"的数据项？')
        .then(() => {
          delWord(ids).then(() => {
            this.getList();
            this.$modal.msgSuccess("删除成功");
          });
        })
        .catch(() => {});
    },
    // 导出
    handleExport() {
      this.download(
        "weiriya/word/export",
        {
          ...this.queryParams,
        },
        `word_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style lang="scss" scoped>
::v-deep p img {
  max-width: 100px;
  max-height: 100px;
  object-fit: cover;
}
.omit {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap
}
.preview-img {
  cursor: pointer;
  width: 100px;
  max-height: 100px;
  margin: 5px;
  vertical-align: top;
}
.el-image-viewer__wrapper {
  z-index: 3000 !important;
}
</style>
